package com.smzd.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.smzd.core.domain.Many2Many;

public class TypeUtil {
    public static int[] stringToIntArr(String string) {
        if (!StringUtils.isEmpty(string)) {
            String[] strArr = string.split(",");
            int[] intArr = new int[strArr.length];
            for (int i = 0; i < strArr.length; i++) {
                intArr[i] = Integer.parseInt(strArr[i]);
            }
            return intArr;
        }
        return null;
    }

    public static Many2ManyWrapper getMany2ManyWrapper(int master, int[] slaves) {
        if (slaves == null || slaves.length == 0) {
            return new Many2ManyWrapper();
        }
        ArrayList<Many2Many> toAdd = new ArrayList<Many2Many>();
        ArrayList<Many2Many> toDel = new ArrayList<Many2Many>();
        for (int slave : slaves) {
            Many2Many mm = new Many2Many();
            mm.setKey1(master);
            if (slave < 0) {
                mm.setKey2(-slave);
                toDel.add(mm);
            } else {
                mm.setKey2(slave);
                toAdd.add(mm);
            }
        }
        return new Many2ManyWrapper(toAdd, toDel);
    }

    public static Many2ManyWrapper getMany2ManyWrapper(int master, String slaves) {
        return getMany2ManyWrapper(master, stringToIntArr(slaves));
    }

    public static class Many2ManyWrapper {
        List<Many2Many> toAdd;
        List<Many2Many> toDel;

        public Many2ManyWrapper() {
            toAdd = new ArrayList<Many2Many>();
            toDel = new ArrayList<Many2Many>();
        }

        public Many2ManyWrapper(List<Many2Many> toAdd, List<Many2Many> toDel) {
            this.toAdd = toAdd;
            this.toDel = toDel;
        }

        public List<Many2Many> getToDel() {
            return toDel;
        }

        public void setToDel(List<Many2Many> toDel) {
            this.toDel = toDel;
        }

        public List<Many2Many> getToAdd() {
            return toAdd;
        }

        public void setToAdd(List<Many2Many> toAdd) {
            this.toAdd = toAdd;
        }
    }
}
